package com.sys.servlet;

import com.sys.dao.sysDao;
import com.sys.data.code;
import com.sys.util.console;
import net.sf.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.sys.util.stringUtils.StringIsEmptyOrNull;

@WebServlet(name = "modifySupplierServlet", urlPatterns = "/sys/modifySupplier", description = "修改供应商")
public class modifySupplierServlet extends sysServlet {

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    setHeadHtml(req, resp, "modifySupplierServlet");
    sysDao dao = new sysDao();
    PrintWriter out = resp.getWriter();
    JSONObject backJson = new JSONObject();

    String supplierId = req.getParameter("supplierId");
    int supplierRep;
    try {
      supplierRep = Integer.valueOf(req.getParameter("supplierRep"));
    } catch (Exception e) {
      e.printStackTrace();
      console.log("信誉填写错误");
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", code.ErrorParam.msg());
      out.print(backJson);
      return;
    }

    if (supplierRep > 4) {
      console.log("信誉超过最大数目");
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", "信誉超过最大数目");
      out.print(backJson);
      return;
    }

    if (StringIsEmptyOrNull(supplierId)) {
      console.log("修改供应商id未填写");
      backJson.put("code", code.ErrorParam.value());
      backJson.put("msg", code.ErrorParam.msg());
      out.print(backJson);
      return;
    }

    dao.modifySupplier(supplierId, supplierRep, out::print);
  }
}
